<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form" lay-filter="editForm">

        <div class="layui-form-item" hidden>
            <label class="layui-form-label required">员工ID</label>
            <div class="layui-input-block">
                <input type="text" name="employeeId" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <img class="layadmin-homepage-pad-img" id="avatarPreview" src="" width="96" height="96"
                style="margin-bottom: 10px;">
            <label class="layui-form-label required">个人头像</label>
            <div class="layui-input-block">
                <input type="input" name="avatar" style="display: none;" id="avatarUpload">
                <button class="layui-btn layui-btn-sm layui-btn-primary" id="uploadAvatarBtn">上传头像</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">全名</label>
            <div class="layui-input-block">
                <input type="text" name="fullName" placeholder="请输入全名" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电子邮件</label>
            <div class="layui-input-block">
                <input type="text" name="email" placeholder="请输入电子邮件" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电话号码</label>
            <div class="layui-input-block">
                <input type="text" name="phone" placeholder="请输入电话号码" value="" class="layui-input">
            </div>
        </div>
        <!--         
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-block">
                <input type="text" name="department" placeholder="请输入部门" value="" class="layui-input">
            </div>
        </div> -->

        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-inline">
                <select id="departmentSelect" name="department" lay-verify="required">
                    <!-- 这里将在JavaScript中动态生成选项 -->
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">职位</label>
            <div class="layui-input-block">
                <input type="text" name="position" placeholder="请输入职位" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">入职日期</label>
            <div class="layui-input-inline">
                <input type="text" name="joinDate" id="joinDate" placeholder="请选择入职日期" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">薪资</label>
            <div class="layui-input-block">
                <input type="text" name="salary" placeholder="请输入薪资" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">操作</label>
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>

    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'laydate'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            var laydate = layui.laydate;

            // 渲染日期选择组件
            laydate.render({
                elem: '#joinDate', // 绑定到指定的input元素
                type: 'date', // 设置日期类型，可选值：year、month、date、time、datetime
                format: 'yyyy-MM-dd HH:mm:ss', // 设置日期格式
                // 其他配置项，可根据需求自行设置
            });




            //接参，回填form表单
            form.getFormVal = function (rowdata) {

                //可以写其他业务请求。
                var data1 = JSON.parse(rowdata);
                console.log(data1)
                // 数据回填
                form.val("editForm", data1);

                // 图片回显
                // 检查输入框是否有值
                var imageUrl = $('#avatarUpload').val();
                if (imageUrl) {
                    // 如果有值，将其回填到<img>的src属性中
                    $('#avatarPreview').attr('src', imageUrl);
                }




                var inputValue = data1.department;



                // 使用Ajax请求获取部门数据
                $.ajax({
                    type: 'GET',
                    url: 'http://localhost:8080/api/job_position?operation=getAllJobPositions',
                    dataType: 'json',
                    success: function (response) {
                        if (response.code === 0) {
                            var jobPositions = response.data; // 假设响应中的数据字段名为data
                            // 获取下拉列表的 DOM 元素
                            var departmentSelect = document.getElementById('departmentSelect');
                            // 遍历响应数据，创建选项并添加到下拉列表中
                            for (var i = 0; i < jobPositions.length; i++) {
                                var option = document.createElement('option');
                                option.value = jobPositions[i].department; // 
                                option.text = jobPositions[i].department; // 假设部门名称字段名为positionName
                                departmentSelect.appendChild(option);
                            }

                            // 设置下拉框的值为 input 的值
                            $('#departmentSelect').val(inputValue);
                            // 使用 layui 的 form 模块重新渲染下拉框，以便选项生效
                            layui.use(['form'], function () {
                                var form = layui.form;
                                form.render('select');
                            });

                        } else {
                            // 请求失败，处理错误情况
                            console.error('请求失败：' + response.msg);
                        }
                    },
                    error: function (error) {
                        // 请求出错，处理错误情况
                        console.error('请求出错：' + JSON.stringify(error));
                    }
                });
            }



            // 监听提交
            form.on('submit(saveBtn)', function (data) {
                console.log(JSON.stringify(data.field))
                // 模拟提交数据到后端
                $.ajax({
                    type: 'POST',
                    url: 'http://localhost:8080/api/employee?operation=update', // 替换成实际的后端接口地址
                    data: JSON.stringify(data.field),
                    contentType: 'application/json',
                    success: function (result) {
                        if (result.code == 0) {
                            // 在父窗口中显示提示消息
                            parent.layer.msg('保存成功', { icon: 1, time: 2000 }); // 提示消息显示2秒
                            // 延迟关闭子窗口，确保提示消息显示完毕
                            setTimeout(function () {
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                // 刷新父页面
                                parent.location.reload();
                            }, 300);
                        } else {
                            layer.msg('保存失败', { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg('请求失败', { icon: 2 });
                    }
                });

                return false;
            });


            var upload = layui.upload;

            // 监听上传按钮的点击事件
            $('#uploadAvatarBtn').on('click', function () {
                $('#avatarUpload').click(); // 触发文件选择框的点击事件
            });

            // 上传头像
            upload.render({
                elem: '#avatarUpload',
                url: 'http://localhost:8080/file/upload', // 替换成实际的上传接口地址
                accept: 'images', // 只允许上传图片
                exts: 'jpg|png|gif|bmp|jpeg',
                done: function (res) {
                    // 直接使用后端返回的 URL 来更新头像预览
                    if (res.code === 1 && res.data.length > 0) {
                        var imageUrl = res.data[0];
                        $('#avatarPreview').attr('src', imageUrl);
                        // 更新隐藏的输入框的值
                        $('#avatarUpload').val(imageUrl);
                    } else {
                        layer.msg('上传失败', { icon: 2 });
                    }
                }
            });



        });
    </script>
</body>

</html>